package com.kurumi.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kurumi.config.MyConfig;
import com.kurumi.mapper.BaseInfoMapper;
import com.kurumi.mapper.MedicalRecordCodingMapper;
import com.kurumi.mapper.MedicalRecordDRGsMapper;
import com.kurumi.mapper.MedicalRecordMapper;
import com.kurumi.pojo.MedicalRecordTrace;
import com.kurumi.pojo.coding_attached.DRGsManageInfo;
import com.kurumi.query.MedicalRecordDRGsQuery;
import com.kurumi.service.MedicalRecordDRGsService;
import com.kurumi.util.DateUtil;
import com.kurumi.util.FileUtil;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.StringUtil;

@Service
public class MedicalRecordDRGsServiceImpl implements MedicalRecordDRGsService {

	@Autowired
	private MedicalRecordDRGsMapper medicalRecordDRGsMapper;
	
	@Autowired
	private MedicalRecordMapper medicalRecordMapper;
	
	@Autowired
	private MedicalRecordCodingMapper medicalRecordCodingMapper;
	
	@Autowired
	private MyConfig myConfig;
	
	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Override
	public List<Map<String, Object>> getMedicalRecordOfDRGsGroup(MedicalRecordDRGsQuery params) {
		// TODO Auto-generated method stub
		return medicalRecordDRGsMapper.getMedicalRecordOfDRGsGroup(params);
	}

	@Override
	public int getMedicalRecordCountOfDRGsGroup(MedicalRecordDRGsQuery params) {
		// TODO Auto-generated method stub
		return medicalRecordDRGsMapper.getMedicalRecordCountOfDRGsGroup(params);
	}

	@Override
	public List<Map<String, Object>> getCodingExport(MedicalRecordDRGsQuery params) {
		// TODO Auto-generated method stub
		return medicalRecordDRGsMapper.getCodingExport(params);
	}

	@Override
	public int getCodingExportCount(MedicalRecordDRGsQuery params) {
		// TODO Auto-generated method stub
		return medicalRecordDRGsMapper.getCodingExportCount(params);
	}

	@Override
	public int uploadDRGsGroup(List<DRGsManageInfo> drgsManageInfos) throws IOException {
		// TODO Auto-generated method stub
		for (DRGsManageInfo drgsManageInfo : drgsManageInfos) {
			List<String> visitGuids = medicalRecordMapper.getVisitGuidByOnlyId(drgsManageInfo.getOnlyId());
			if(!visitGuids.isEmpty()){
				String visitGuid = visitGuids.get(0);
				drgsManageInfo.setVisitGuid(visitGuid);
				String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
				String versionFilePath =filePath+"version\\";
				String fileName = visitGuid + ".json";
				String versionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
				List<String> jsonDatas = medicalRecordMapper.getMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
				Map<String, Object> jsonMap = new HashMap<String, Object>();
				if(!jsonDatas.isEmpty()){
					jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
					if(jsonMap == null){
						jsonMap = new HashMap<String, Object>();
					}
				}
				Subject subject=SecurityUtils.getSubject();
				Session session = subject.getSession();
				Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
				String userCode =(String)currentUser.get("user_code");
				String userName =(String)currentUser.get("user_name");
				jsonMap.put("userCode", userCode);
				jsonMap.put("userName", userName);
				List<Map<String,Object>> drgsGroups = baseInfoMapper.getDRGsGroupByQueryName(drgsManageInfo.getLocalDRGsCode());
				if(!drgsGroups.isEmpty()){
					drgsManageInfo.setLocalDRGsName((String)drgsGroups.get(0).get("label"));
					BigDecimal localDRGsGroupWeight=getBigDecimalValue(drgsGroups.get(0).get("group_weight"));
					drgsManageInfo.setLocalDRGsGroupWeight(localDRGsGroupWeight);
					if(drgsManageInfo.getLocalDRGsGroupTotalWeight() == null){
						drgsManageInfo.setLocalDRGsGroupTotalWeight(localDRGsGroupWeight);
					}
					
					
				}
				drgsManageInfo.setWhetherDRGsManageCode("1");
				drgsManageInfo.setWhetherDRGsManageName("是");
				if(drgsManageInfo.getWhetherMDTName() == null){
					drgsManageInfo.setWhetherMDTCode("2");
					drgsManageInfo.setWhetherMDTName("否");
				}else{
					if("是".equals(drgsManageInfo.getWhetherMDTName())){
						drgsManageInfo.setWhetherMDTCode("1");
						drgsManageInfo.setWhetherMDTName("是");
					}else{
						drgsManageInfo.setWhetherMDTCode("2");
						drgsManageInfo.setWhetherMDTName("否");
					}
				}
				jsonMap.put("drgsManageInfo", drgsManageInfo);
				String jsonMapJson = JsonUtil.objectToJson(jsonMap);
				FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
				FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
				List<String> medicalRecordJsons = medicalRecordMapper.getMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
				List<Map<String,Object>> treatmentTraceTypes = baseInfoMapper.getTraceTypeByCode(MedicalRecordTrace.VISIT_DRGS);
				if(treatmentTraceTypes.size() == 0){
					return 0;
				}
				
				if(medicalRecordMapper.getMeditalRecordTraceCount(visitGuid, MedicalRecordTrace.VISIT_DRGS) == 0){
					MedicalRecordTrace medicalRecordTrace = new MedicalRecordTrace();
					medicalRecordTrace.setVisitGuid(visitGuid);
					
					medicalRecordTrace.setCreateUserId(userCode);
					medicalRecordTrace.setCreateUserName(userName);
					medicalRecordTrace.setTraceTypeCode((String)treatmentTraceTypes.get(0).get("code"));
					medicalRecordTrace.setTraceTypeName((String)treatmentTraceTypes.get(0).get("name"));
					
					medicalRecordMapper.insertMedicalRecordTrace(medicalRecordTrace);
				}
				if(medicalRecordJsons.isEmpty()){
					medicalRecordMapper.insertMedicalRecordJson(jsonMapJson);
				}else{
					medicalRecordMapper.deleteMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
					medicalRecordMapper.insertMedicalRecordJson(jsonMapJson);
				}
				if(medicalRecordCodingMapper.getSolrUpdateCount(visitGuid) == 0){
					medicalRecordCodingMapper.insertSolrUpdate(visitGuid);
				}
			}
		}
		return 1;
	}
	
	private BigDecimal getBigDecimalValue(Object obj){
		if(obj == null){
			return null;
		}
		if(obj instanceof BigDecimal){
			return (BigDecimal)obj;
		}else if(obj instanceof Double){
			return new BigDecimal((Double)obj);
		}
		else if(obj instanceof Integer){
			return new BigDecimal((Integer)obj);
		}
		else if(obj instanceof String){
			return new BigDecimal((String)obj);
		}
		else{
			return null;
		}
	}

}
